package com.engine.annual.web;

import com.alibaba.fastjson.JSON;
import com.engine.annual.service.AnnualService;
import com.engine.annual.service.impl.AnnualServiceImpl;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.awt.*;
import java.util.HashMap;

public class AnnualAction {

    private AnnualService getService(User user) {
        //实例化Service类
        return ServiceUtil.getService(AnnualServiceImpl.class, user);
    }

    /**
     * 查询
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getAnnualInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getAnnualInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        HashMap<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas.putAll(getService(user).getAnnualService(ParamUtil.request2Map(request)));
            apidatas.put("api_status", true);

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception: " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }


    /**
     * 添加
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/addAnnualInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String addAnnualInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        HashMap<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas.putAll(getService(user).addAnnualService(ParamUtil.request2Map(request)));
            apidatas.put("api_status", true);

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception: " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    /**
     * 修改
     * @param request
     * @param response
     * @return
     */
    @PUT
    @Path("/updAnnualInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String updAnnualInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        HashMap<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas.putAll(getService(user).updAnnualService(ParamUtil.request2Map(request)));
            apidatas.put("api_status", true);

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception: " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }

    /**
     * 删除
     * @param request
     * @param response
     * @return
     */
    @DELETE
    @Path("/delAnnualInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String delAnnualInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        HashMap<String, Object> apidatas = new HashMap<>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            apidatas.putAll(getService(user).delAnnualService(ParamUtil.request2Map(request)));
            apidatas.put("api_status", true);

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception: " + e.getMessage());
        }
        return JSON.toJSONString(apidatas);
    }


}
